﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Profile;
using System.Data;

namespace WebSite.admin.product
{
    public partial class goods_add05 : System.Web.UI.Page
    {
        // Fields

        // Methods
        private void AddItemFromSourceListBox(ListBox sourceBox, ListBox targetBox, string aa)
        {
            string str = "0";
            foreach (ListItem item in targetBox.Items)
            {
                if (sourceBox.SelectedValue.ToString() == item.Value.ToString())
                {
                    str = "1";
                    break;
                }
            }
            if (aa.ToString() == "1")
            {
                foreach (ListItem item2 in sourceBox.Items)
                {
                    if ((item2.Selected && !targetBox.Items.Contains(item2)) && (str.ToString() == "0"))
                    {
                        targetBox.Items.Add(new ListItem(string.Concat(new object[] { item2, "-", this.Radio1.SelectedValue.ToString(), "" }), "" + sourceBox.SelectedValue.ToString() + ""));
                    }
                }
            }
            else
            {
                foreach (ListItem item3 in sourceBox.Items)
                {
                    if (!targetBox.Items.Contains(item3) && (str.ToString() == "0"))
                    {
                        targetBox.Items.Add(new ListItem(string.Concat(new object[] { item3, "-", this.Radio1.SelectedValue.ToString(), "" }), "" + item3.Value + ""));
                    }
                }
            }
        }

        private void Bind()
        {
            string sql = "select * from DB_Pinpai where visible=1 and languages=" + info.languages("") + " order by pinpaiorder";
            DataTable table = new DataTable();
            table = SQLAccess.GreatDs(sql).Tables[0];
            if (table.Rows.Count > 0)
            {
                foreach (DataRow row in table.Rows)
                {
                    this.propinpai.Items.Add(new ListItem(row["pinpainame"].ToString(), row["pinpaiid"].ToString()));
                }
            }
        }

        protected void btnToAllLeft_Click(object sender, EventArgs e)
        {
            foreach (ListItem item in this.lbRight.Items)
            {
                DataTable table = SQLAccess.GreatDs("select * from DB_Proxian where proids=" + item.Value + " and proid=" + base.Request.QueryString["pid"].ToString() + "").Tables[0];
                if (table.Rows.Count > 0)
                {
                    if (table.Rows[0]["lian"].ToString() == "1")
                    {
                        SQLAccess.DoSql("delete from DB_Proxian where proid=" + item.Value + " and  proids=" + base.Request.QueryString["pid"].ToString() + "");
                        SQLAccess.DoSql("delete from DB_Proxian where proids=" + item.Value + " and  proid=" + base.Request.QueryString["pid"].ToString() + "");
                    }
                    else
                    {
                        SQLAccess.DoSql("delete from DB_Proxian where proids=" + item.Value + " and  proid=" + base.Request.QueryString["pid"].ToString() + "");
                    }
                }
            }
            this.RemoveSelectedItemAll(this.lbRight);
        }

        protected void btnToAllRight_Click(object sender, EventArgs e)
        {
            string aa = "";
            if (this.lbLeft.Items.Count > 0)
            {
                this.AddItemFromSourceListBox(this.lbLeft, this.lbRight, aa);
            }
        }

        protected void btnToLeft_Click(object sender, EventArgs e)
        {
            if (this.lbRight.SelectedItem != null)
            {
                DataTable table = SQLAccess.GreatDs("select * from DB_Proxian where proids=" + this.lbRight.SelectedValue.ToString() + " and proid=" + base.Request.QueryString["pid"].ToString() + "").Tables[0];
                if (table.Rows.Count > 0)
                {
                    if (table.Rows[0]["lian"].ToString() == "1")
                    {
                        SQLAccess.DoSql("delete from DB_Proxian where proid=" + this.lbRight.SelectedValue.ToString() + " and  proids=" + base.Request.QueryString["pid"].ToString() + "");
                        SQLAccess.DoSql("delete from DB_Proxian where proids=" + this.lbRight.SelectedValue.ToString() + " and  proid=" + base.Request.QueryString["pid"].ToString() + "");
                    }
                    else
                    {
                        SQLAccess.DoSql("delete from DB_Proxian where proids=" + this.lbRight.SelectedValue.ToString() + " and  proid=" + base.Request.QueryString["pid"].ToString() + "");
                    }
                }
                this.RemoveSelectedItem(this.lbRight);
            }
        }

        protected void btnToRight_Click(object sender, EventArgs e)
        {
            string aa = "1";
            if (this.lbLeft.SelectedItem != null)
            {
                this.AddItemFromSourceListBox(this.lbLeft, this.lbRight, aa);
            }
        }

        private void Display(string parentid, string space)
        {
            string sql = "Select * From DB_Productclass Where parentid =" + parentid + " and languages=" + info.languages("") + " Order By classorder";
            DataTable table = new DataTable();
            table = SQLAccess.GreatDs(sql).Tables[0];
            if (table.Rows.Count > 0)
            {
                foreach (DataRow row in table.Rows)
                {
                    this.typeid.Items.Add(new ListItem(space + "" + row["classname"].ToString() + "", "" + row["classid"].ToString() + ""));
                    this.Display(row["classid"].ToString(), "|-" + space);
                }
            }
        }

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            if (base.Request.QueryString["pid"] != null)
            {
                for (int i = 0; i < this.lbRight.Items.Count; i++)
                {
                    string[] strArray = this.lbRight.Items[i].Text.ToString().Split(new char[] { '-' });
                    string sql = "Select * From DB_Proxian Where ((proid =" + base.Request.QueryString["pid"].ToString() + " and proids=" + this.lbRight.Items[i].Value.ToString() + ") or (proid =" + this.lbRight.Items[i].Value.ToString() + " and proids=" + base.Request.QueryString["pid"].ToString() + "))";
                    DataTable table = new DataTable();
                    table = SQLAccess.GreatDs(sql).Tables[0];
                    if (table.Rows.Count > 0)
                    {
                        SQLAccess.DoSql("delete from DB_Proxian where proxianid=" + table.Rows[0]["proxianid"].ToString() + "");
                    }
                    if (strArray[1].ToString().Trim() == "[双向关联]")
                    {
                        SQLAccess.DoSql("insert into DB_Proxian (proid,proids,pronames,lian) values (" + base.Request.QueryString["pid"].ToString() + "," + this.lbRight.Items[i].Value.ToString() + ",'" + strArray[0].ToString() + "',1)");
                        SQLAccess.DoSql("insert into DB_Proxian (proid,proids,pronames,lian) values (" + this.lbRight.Items[i].Value.ToString() + "," + base.Request.QueryString["pid"].ToString() + ",'" + strArray[0].ToString() + "',1)");
                    }
                    else
                    {
                        SQLAccess.DoSql("insert into DB_Proxian (proid,proids,pronames,lian) values (" + base.Request.QueryString["pid"].ToString() + "," + this.lbRight.Items[i].Value.ToString() + ",'" + strArray[0].ToString() + "',0)");
                    }
                }
                base.Response.Write("<script language='javascript'>window.location.href='goods_add05.aspx?pid=" + base.Request.QueryString["pid"] + "';</script>");
                base.Response.End();
            }
        }

        protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
        {
            string str = "0";
            string sql = "select * from DB_Shoppro where visible=1 and languages=" + info.languages("") + " ";
            if (base.Request.QueryString["pid"] != null)
            {
                sql = sql + " and proid<>" + base.Request.QueryString["pid"].ToString() + "";
            }
            if (this.typeid.SelectedValue.ToString() != "0")
            {
                sql = sql + " and typeid=" + this.typeid.SelectedValue.ToString() + "";
            }
            if (this.propinpai.SelectedValue.ToString() != "0")
            {
                sql = sql + " and propinpai=" + this.propinpai.SelectedValue.ToString() + "";
            }
            if (re.replace(this.keyname.Value.ToString()) != "")
            {
                sql = sql + " and (proname like '%" + re.replace(this.keyname.Value.ToString()) + "%' or procode like '%" + re.replace(this.keyname.Value.ToString()) + "%')";
            }
            DataTable table = new DataTable();
            table = SQLAccess.GreatDs(sql).Tables[0];
            if (table.Rows.Count > 0)
            {
                foreach (DataRow row in table.Rows)
                {
                    for (int i = 0; i < this.lbLeft.Items.Count; i++)
                    {
                        if (row["proid"].ToString() == this.lbLeft.Items[i].Value.ToString())
                        {
                            str = "1";
                            break;
                        }
                    }
                    if (str.ToString() == "0")
                    {
                        this.lbLeft.Items.Add(new ListItem(row["proname"].ToString(), row["proid"].ToString()));
                    }
                }
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (this.Session["Adminlogin"] == null)
            {
                base.Response.Write("<script language=javascript>window.top.location.href='../login.aspx';</script>");
                base.Response.End();
            }
            this.ImageButton1.Attributes["onclick"] = "return clicka();";
            if (!base.IsPostBack)
            {
                if (base.Request.QueryString["pid"] != null)
                {
                    this.upbind();
                }
                this.Display("0", "|--");
                this.Bind();
            }
        }

        private void RemoveSelectedItem(ListBox listControl)
        {
            while (listControl.SelectedIndex != -1)
            {
                listControl.Items.RemoveAt(listControl.SelectedIndex);
            }
        }

        private void RemoveSelectedItemAll(ListBox listControl)
        {
            if (listControl.Items.Count > 0)
            {
                listControl.Items.Clear();
            }
        }

        private void upbind()
        {
            string sql = "select * from DB_Proxian where proid=" + base.Request.QueryString["pid"];
            DataTable table = new DataTable();
            table = SQLAccess.GreatDs(sql).Tables[0];
            if (table.Rows.Count > 0)
            {
                foreach (DataRow row in table.Rows)
                {
                    if (row["lian"].ToString() == "1")
                    {
                        this.lbRight.Items.Add(new ListItem(row["pronames"].ToString() + "-[双向关联]", row["proids"].ToString()));
                    }
                    else
                    {
                        this.lbRight.Items.Add(new ListItem(row["pronames"].ToString() + "-[单向关联]", row["proids"].ToString()));
                    }
                }
            }
        }

        // Properties
        protected global_asax ApplicationInstance
        {
            get
            {
                return (global_asax)this.Context.ApplicationInstance;
            }
        }

        protected DefaultProfile Profile
        {
            get
            {
                return (DefaultProfile)this.Context.Profile;
            }
        }

    }
}